package cn.dy.dao.impl;

import cn.dy.dao.EmpDao;
import cn.dy.pojo.Emp;
import cn.dy.util.JDBCUtil;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class EmpDaoImpl implements EmpDao {
    private JdbcTemplate jdbcTemplate=new JdbcTemplate(JDBCUtil.getDataSource());
    @Override
    public List<Emp> selectAll() {
       List<Emp> emplist=jdbcTemplate.query("select * from emp",new BeanPropertyRowMapper<>(Emp.class));
        return emplist;
    }

    @Override
    public Emp selectById(int id) {
        List<Emp> list = jdbcTemplate.query("select * from emp where id = ?", new BeanPropertyRowMapper<>(Emp.class), id);
        return list.get(0);
    }

    @Override
    public void add(Emp emp) {
        jdbcTemplate.update("insert into emp values (null,?,?,?,?,?)",emp.getName(),emp.getSex(),emp.getJoinday(),emp.getSalary(),emp.getDept_id());
    }

    @Override
    public void delete(int id) {
        int i = jdbcTemplate.update("delete from emp where id = ?", id);
        if (i>0){
            System.out.println("删除成功");
        }
    }

    @Override
    public void update(Emp emo) {
        jdbcTemplate.update("update emp set name=?,sex=?,joinday=?,salary=?,dept_id=? where id=?",emo.getName(),emo.getSex(),emo.getJoinday(),emo.getSalary(),emo.getDept_id(),emo.getId());
    }
}
